package presenters;

import android.app.Activity;
import android.view.View;
import data.Account;
import data.AddAccount;
import data.DBInterfaceError;
import views.IFieldView;

/**
 * Handles logic and button presses from view
 * 
 * @author Kelsey
 */
public class CreateAccountPresenter {

	/** The view of CreateAccount page */
	private final IFieldView myView;

	/** Activity being used */
	private final Activity activity;

	/**
	 * Creates a presenter for CreateAccount
	 * 
	 * @param view
	 *            - a fieldview view
	 */
	public CreateAccountPresenter(IFieldView view) {
		myView = view;
		activity = (Activity) view;
	}

	/**
	 * this gets called when the onAccept click button gets clicked
	 * 
	 * @param name
	 *            - the name
	 * @param email
	 *            - the email
	 * @param phone
	 *            - the phone
	 * @param accName
	 *            - the account name
	 * @param pass
	 *            - the password
	 * @param source
	 *            - the source
	 */
	public void onAcceptClick(String name, String email, String phone,
			String accName, String pass, View source) {
		final Account account = new Account();
		account.setAccountName(accName);
		account.setPhoneNumber(phone);
		account.setEmail(email);
		account.setRealName(name);
		account.setPassword(pass);
		boolean success = false;
		try {
			success = new AddAccount(activity, account).getResult();
		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}
		if (success) {
			myView.showToast("success");
			myView.advance(source);
		} else {
			myView.showToast("failure");
		}
	}

	/**
	 * 
	 * this gets called when the button to cancel is clicked
	 * 
	 * @param source
	 *            - the view source
	 */
	public void onCancelCreateAccountClick(View source) {
		myView.advance(source);
	}

	/**
	 * hides the soft keyboard
	 */
	public void hideSoftKeyboard() {
		myView.hideSoftKeyboard(activity.getCurrentFocus());
	}
}